package actions;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import utils.MessageViewer;
import utils.RSA_Password;
import client.ClientApplication;
import client.ClientBindObject;
import client.ClientSocketConnection;
import client_gui.ClientWindow;

public class UnregisterAction extends RLMAbstractAction {

	public UnregisterAction(ClientWindow owner) {
		setActionOwner(owner);
//		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0));
		putValue(SMALL_ICON, loadIcon("images/unreg.png"));
		putValue(NAME, "Unregister");
		putValue(SHORT_DESCRIPTION, "Deregistrovanje");
	}
	
	
	public void actionPerformed(ActionEvent e) {
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Password:");
	    JPasswordField passwordField = new JPasswordField();
	    passwordField.setPreferredSize(new Dimension(150,20));
	    
	    panel.add(label);
	    panel.add(passwordField);
	    
	    int action = JOptionPane.showConfirmDialog(getActionOwner(), panel, "Enter Password", JOptionPane.OK_CANCEL_OPTION);
		
	    if (action != JOptionPane.OK_OPTION) {
	    	return;
	    }
	    
	    RSA_Password rsa = new RSA_Password();
	    
	    String password = new String(passwordField.getPassword());
	    password = rsa.codeData(password);
	    
	    if ( !password.equals(getActionOwner().getApplicaton().getClientUser().getPassword()) ) {
	    	MessageViewer.showError(getActionOwner(), "Password and Confirmed password must be the same.");
	    	return;
	    }
	    
	    
		ClientSocketConnection connection = getActionOwner().getApplicaton().retrieveSocketConnection();
		
		String unregCommand = "UNREG " + getActionOwner().getApplicaton().getClientUser().getUsername() +
								   " " + getActionOwner().getApplicaton().getClientUser().getPassword();

		connection.setMessage(UNREG_CMD, unregCommand);
		
//		String response = connection.buffers[UNREG_CMD].read();
		
//		if ( response.startsWith("100") ) {
			
			getActionOwner().getApplicaton().setState(INITIAL_STATE);
			
			Set<String> keys = ClientApplication.clientBinding.keySet();
			
			Iterator<String> keysIterator = keys.iterator();
			
			while ( keysIterator.hasNext() ) {
				String key = keysIterator.next();

				ClientBindObject cbo = ClientApplication.clientBinding.get(key);
				if (cbo.getDialog() != null) {
					cbo.getDialog().setConnection(null);
					cbo.getDialog().dispose();
				}
				if (cbo.getConnection() != null)
					cbo.getConnection().quitConnection();
				cbo.setConnection(null);
			}
			
			ClientApplication.clientBinding.clear();
			
			getActionOwner().getApplicaton().getClientModel().clearModel();
			
			getActionOwner().refreshWindow();
			
			
			connection.quitConnection();
			getActionOwner().getApplicaton().setSocketConnection(null);
			connection = null;
//		}
	}
	
}
